<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="分类名" prop="name">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" :min="0" :max="2147483647"></el-input-number>
        <br />
        <span>小提示：数值越大越靠前</span>
      </el-form-item>
      <el-form-item label="是否显示" prop="isDisplay">
        <el-switch v-model="dataForm.isDisplay" active-color="#13ce66" inactive-color="#999"></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        parentId: '',
        parentName: '',
        name: '',
        sort: 0,
        isDisplay: true
      },
      loading: false,
      cateListTreeProps: {
        label: 'name',
        children: 'children'
      },
      dataRule: {
        name: [{ required: true, message: '新闻标题不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(async () => {
        this.$refs['dataForm'].resetFields()

        if (!this.dataForm.id) return
        var res = await this.$http.post(
          `/news-admin/category/info/${this.dataForm.id}`
        )
        if (res && res.code !== 200) return
        this.dataForm = res.data
        this.dataForm.isDisplay = this.dataForm.isDisplay === 1
        this.dataForm.name = res.data.name
        this.dataForm.sort = res.data.sort
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(async valid => {
        if (!valid) return
        this.loading = true
        let res = await this.$http({
          url: this.$http.adornUrl(
            `/news-admin/category/${!this.dataForm.id ? 'save' : 'update'}`
          ),
          method: 'post',
          data: {
            id: this.dataForm.id || undefined,
            parentId: this.dataForm.parentId,
            name: this.dataForm.name,
            sort: this.dataForm.sort,
            isDisplay: this.dataForm.isDisplay ? 1 : 0
          }
        })

        if (res && res.code === 200) {
          this.$notify.success({ title: '成功', message: '保存新闻分类成功' })
          this.visible = false
          this.$emit('refreshDataList')
        } else {
          this.$notify.error({ title: '错误', message: res.msg })
        }
        this.loading = false
      })
    },
    handlerCateChange(val) {
      if (val && val.length > 0) {
        this.dataForm.parentId = val[val.length - 1]
      } else {
        this.dataForm.parentId = 0
      }
    },
    // 菜单树选中
    cateListTreeCurrentChangeHandle(data, node) {
      this.dataForm.parentId = data.id
      this.dataForm.parentName = data.name
    },
    // 菜单树设置当前选中节点
    cateListTreeSetCurrentNode() {
      this.$refs.cateListTree.setCurrentKey(this.dataForm.parentId)
      this.dataForm.parentName = (this.$refs.cateListTree.getCurrentNode() ||
        {})['name']
    }
  }
}
</script>

<style scoped>
</style>
